<?php
/**
 * 【司机处罚表】
 * Created by PhpStorm.
 * User: SK
 * Date: 2018-06-08
 * Time: 15:41
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Support\Facades\DB;
use App\Http\model\Curdpost;

class DisciplinedControllwer extends Controller
{

    /**
     * 【司机处罚列表】
     * @Author sk
     * @DateTime 2018-06-08
     * @param Request $request
     * @return array
     */
    public function index(Request $request)
    {
        $array = $request->all();
        $select = [
            'disciplined.driver_id',//司机ID
            'disciplined.admin_user',//修改人
            'disciplined.update_time',//更新时间
            'disciplined.punished_time',//处罚时间
            'disciplined.punished_where',//处罚原因
            'disciplined.punished_action',//处罚结果

            'driver.licenseId',//驾驶证号码
            'driver.driver_name',//司机姓名
            'driver.driver_phone',//司机手机号
            'driver.sex',//性别 :1男 2女
            'driver.convoy_id',//车队id
            'driver.car_id',//车辆id
            'car_car.num',//车牌号
            'car_car.typeid',//车系id
            'car_series.name as sname',//车系名称
            'convoy.name',//车队名称
            'convoy.branch_id',
            'branch.company_name',//机构名称
            'branch.type'//机构类型
        ];
        $handle = DB::table('disciplined')
            ->join('driver', 'disciplined.driver_id', '=', 'driver.id')//差司机
            ->join('convoy', 'driver.convoy_id', '=', 'convoy.id')//车队
            ->join('branch', 'convoy.branch_id', '=', 'branch.id')//机构
            ->join('car_car', 'driver.car_id', '=', 'car_car.id')//车牌号
            ->join('car_series', 'car_car.typeid', '=', 'car_series.id')//车系
            // ->whereBetween('driver.save_time', [strtotime($start), strtotime($end)])
            ->select($select);
        foreach ($array as $key => $val) {
            $array[$key] && $handle->where($key, 'like', '%' . $val . '%');
        }
        $datas = $handle->get()->toarray();
        // dd($datas);
        $data = json_encode($datas);
        return $data ? response(['data' => $data, 'code' => 1000, 'message' => '信息返回成功！'], 200) : response(['data' => '', 'code' => 1011, 'message' => '信息返回失败！'], 400);

    }

    /**【查询公司id和车队id】
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function create()
    {
        $hht = Db::table('driver')
            ->join('convoy', 'driver.convoy_id', '=', 'convoy.id')//车队
            ->join('branch', 'convoy.branch_id', '=', 'branch.id');//机构;
        $name = $hht->select('name', 'convoy.id as convoy_id')->distinct()->get();
        $company_name = $hht->select('company_name', 'branch.id as branch_id ')->get();
        $name = json_decode(json_encode($name), true);
        $company_name = json_decode(json_encode($company_name), true);
        return $name ? response(['name' => $name, 'company_name' => $company_name, 'message' => '成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '失败', 'code' => 1011], 400);
    }

    /**
     * 【添加司机列表】
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function showadd(Request $request)
    {
        $array = $request->all();
        if (isset($array['s'])) {
            unset($array['s']);
        }
        if ($request['branch_id']) {
//根据公司id查询
            $branch = DB::table('convoy')->where('convoy.branch_id', $request['branch_id'])
                ->join('driver', 'driver.convoy_id', '=', 'convoy.id');
            $bb = $branch->get();
            $msg = json_decode(json_encode($bb), true);
            return $msg ? response(['msg' => $msg,
                'message' => '成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '失败', 'code' => 1011], 400);
        } elseif ($request['convoy_id']) {
//根据车队对查询
            $convoy = DB::table('driver')->where('driver.convoy_id', $request['convoy_id']);
            $cc = $convoy->get();
            $msg = json_decode(json_encode($cc), true);
            return $msg ? response(['msg' => $msg,
                'message' => '成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '失败', 'code' => 1011], 400);
        } else {
            $handle = Db::table('driver')
                ->join('convoy', 'driver.convoy_id', '=', 'convoy.id')//车队
                ->join('branch', 'convoy.branch_id', '=', 'branch.id');//机构;
            foreach ($array as $key => $val) {
                $array[$key] && $handle->where($key, 'like', '%' . $val . '%');
            }
            $datas = $handle->get();
        }
        $msg = json_decode(json_encode($datas), true);
        return $msg ? response(['msg' => $msg,
            'message' => '成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '失败', 'code' => 1011], 400);
    }

    /**
     * 【司机处罚添加add】
     * @Author sk
     * @DateTime 2018-06-08
     * @type  int 1添加
     * @param Request $request
     * @return array
     */
    public function add(Request $request)
    {
        $data = $request->all();
        if (empty($data['licenseId']) || empty($data['punished_time']) || empty($data['punished_where']) || empty($data['driver_id'])) {
            return '缺少参数';
        }
        try {
            /*$add['licenseId'] = $data['licenseId'];//驾驶照号码
            $add['punished_time'] = $data['punished_time'];//处罚时间
            $add['punished_where'] = $data['punished_where'];//原因
            $add['punished_action'] = $data['punished_action'];//结果
            $add['driver_id'] = $data['driver_id'];//司机id
            $add['admin_user'] = $data['admin_user'];//修改人*/
            $data['update_time'] = time();
            $msg = Db::table('disciplined')->insert($data);
            if ($msg) {
                $all = ['CompanyId' => 'test', 'LicenseId' => $data['licenseId'], 'PunishTime' => $data['punished_time'], 'PunishResult' => $data['punished_action']];
                $news = new Curdpost();
                $news->NewInsert("rateddriverpunish", $all);
            }
            $msg = json_decode(json_encode($msg), true);
            return $msg ? response(['msg' => $msg, 'message' => '添加成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '添加失败', 'code' => 1011], 400);
        } catch (JWTException $e) {
            return response(['msg' => '', 'code' => 1011, 'message' => '验证不通过']);
        }


    }

    /**
     * 【司机处罚添加查询司机方法】
     * @Author sk
     * @DateTime 2018-06-08
     * @param Request $request
     * @return array
     */
    public function show(Request $request)
    {
        $keys = $request->all();
        $handle = DB::table('driver');
        foreach ($keys as $key => $val) {
            $keys[$key] && $handle->where($key, 'like', '%' . $val . '%');
        }
        $datas = $handle->get()->toarray();
        $data = json_encode($datas);
        return response(['mag' => $data, 'code' => 1000, 'message' => '数据返回成功！'], 200);
        //  dd($datas);
        /*
                if (!empty($driver_name)) {
                    $where1 = ['driver_name', 'like', "%$driver_name%"];
                    $where[] = $where1;
                }
                if (!empty($driver_phone)) {
                    $where2 = ['driver_phone', 'like', "%$driver_phone%"];
                    $where[] = $where2;
                }
                $info = DB::table('driver')->where($where)->get()->toArray();*/


    }

}